package services;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Binder;
import android.os.IBinder;

import com.huii.huiiplayer.helper.PlayHelper;

public class MyPlayService extends Service implements OnCompletionListener,
		OnPreparedListener {

	private MediaPlayer mPlayer;
	private PlayHelper helper;
	// // 广播接收器的action
	// public static final String START = "com.servieces.MyPlayerService.START";
	// public static final String PAUSE = "com.servieces.MyPlayerService.PAUSE";
	// public static final String NEXT = "com.servieces.MyPlayerService.NEXT";
	// public static final String PRV = "com.servieces.MyPlayerService.PRV";
	// public static final String CIRCLE =
	// "com.servieces.MyPlayerService.CIRCLE";

	// // 判断当前的音乐是不是第一首
	// private boolean isfirst = true;
	//
	// // extra的key
	// public static final String KEY_PATH = "path";

	// 判断是否暂停
	public boolean isPause;

	// 当前播放的音乐位置
	// int currPosition;
	// 上一首音乐的位置
	// int lastPosition;

	public class MyBinder extends Binder {

		public MyPlayService getService() {
			return MyPlayService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub

		return new MyBinder();
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		initMediaPlayer();
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub

		return START_STICKY;
	}

	// 初始化Service的playhelper
	public void setPlayHelper(PlayHelper helper) {
		this.helper = helper.attachToService(this);
	}

	// 初始化MediaPlayer
	private void initMediaPlayer() {

		mPlayer = new MediaPlayer();
		// 注册播放完的监听
		mPlayer.setOnCompletionListener(this);
		// 注册准备完监听
		mPlayer.setOnPreparedListener(this);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		// TODO Auto-generated method stub
		mPlayer.start();
	}

	@Override
	public void onCompletion(MediaPlayer mp) {

		helper.completion();
	}

	// 开始播放
	public boolean start() {
		// this.currPosition = position;
		if (isPause) {
			mPlayer.start();
			isPause = false;
		} else {
			mPlayer.reset();
			try {
				mPlayer.setDataSource(helper.getSongPath());
				mPlayer.prepare();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return true;
	}

	public void pause() {
		if (!mPlayer.isPlaying()) {
			mPlayer.pause();
			isPause = true;
		}
	}

	public void next() {
		// int nextPosition = getNextPosition();
		helper.getNextPosition();
		start();
	}

	public void prv() {
		helper.getLastPosition();
		start();
	}

	public void setLoop() {
		if (mPlayer.isLooping()) {
			mPlayer.setLooping(false);
		} else {
			mPlayer.setLooping(true);
		}
	}
}
